package com.bh.springbootjdk17demo.controller;

import com.bh.springbootjdk17demo.entity.Sjzd;
import com.bh.springbootjdk17demo.service.SjzdService;
import jakarta.annotation.Resource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author sy
 * @version 1.0
 * @description slf4j测试
 */
@Slf4j
@Data
@RestController
@RequestMapping("/sjzd")
public class MyController {

    private String msg = "Hello, Lombok and SLF4J";
    @Resource
    private SjzdService sjzdService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    //http://localhost:8006/sjzd/msg
    @GetMapping("/msg")
    public String getMsg() {
        log.info("return msg:{}", msg);
        return msg;
    }

    //POSTMAN
    @PostMapping("/getMsgByPost")
    public Map<String, Object> getMsgByPost(@RequestBody Map<String, Object> map) {
        log.info("==json=={}", map);
        return map;
    }

    //http://localhost:8006/sjzd/getList
    @GetMapping("/getList")
    public Map<String, Object> getList(Sjzd sjzd) {
        List<Sjzd> list = sjzdService.getList(sjzd);
        redisTemplate.opsForValue().set("sjzdList", list, 60, TimeUnit.SECONDS);
        Map<String, Object> result = new HashMap<>();
        result.put("data", list);
        return result;
    }

    //http://localhost:8006/sjzd/redis?token=123
    @GetMapping("/redis")
    public String handleRedis(String token) {
        redisTemplate.opsForValue().set("token", token, 60, TimeUnit.SECONDS);
        return token;
    }

}
